home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / Plug-in - WireFrame Renderer / SR_Marker.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  6.8 KB  |  254 lines  |  [TEXT/MPS ]

  1. /******************************************************************************
  2.  **                                                                             **
  3.  **     Module:        SR_Marker.c                                                 **
  4.  **                                                                          **
  5.  **                                                                          **
  6.  **     Purpose:     This is the entry point into the renderer for markers.     **
  7.  **                                                                          **
  8.  **                                                                          **
  9.  **                                                                          **
  10.  **     Copyright (C) 1996 Apple Computer, Inc.  All rights reserved.          **
  11.  **                                                                          **
  12.  **                                                                          **
  13.  *****************************************************************************/
  14. #include <assert.h>
  15. #include <stdlib.h>
  16.  
  17. #include "QD3D.h"
  18. #include "QD3DMath.h"
  19. #include "QD3DStyle.h"
  20. #include "QD3DSet.h"
  21. #include "QD3DView.h"
  22.  
  23. #include "SR.h"
  24. #include "SR_Marker.h"
  25.  
  26.  
  27. /*===========================================================================*\
  28.  *
  29.  *    Routine    :    SR_Geometry_Marker()
  30.  *
  31.  *    Comment    :    This function is the marker renderer entry point, it 
  32.  *                collects the state of escher, updates the state of the world
  33.  *                and then calls the appropiate rendering function.
  34.  *
  35. \*===========================================================================*/
  36.  
  37. TQ3Status SR_Geometry_Marker(
  38.     TQ3ViewObject             view, 
  39.     TSRPrivate                 *srPrivate,
  40.     TQ3GeometryObject         marker,
  41.     const TQ3MarkerData        *markerData)
  42. {
  43.     TQ3ColorRGB                color;
  44.     TQ3Boolean                highlightState;
  45.     TQ3XClipMaskState        clipMaskState;
  46.     
  47.     UNUSED(marker);
  48.  
  49.     assert(view         != NULL);
  50.     assert(srPrivate    != NULL);
  51.     assert(markerData    != NULL);
  52.  
  53.     /*
  54.      *  Call the application's idle progress method, via the view.  If
  55.      *  the app's method returns kQ3Failure, then we don't go on.
  56.      */
  57.     if (SR_IdleProgress(view, srPrivate) == kQ3Failure) {
  58.         return (kQ3Success);
  59.     }
  60.  
  61.     if (srPrivate->drawRegion == NULL) {
  62.         return (kQ3Success);
  63.     }
  64.     
  65.     /*
  66.      *  Find out if we're clipped out or not. No reason to go any
  67.      *  further if the region is obscured or entirely off-screen. 
  68.      */
  69.     Q3XDrawRegion_GetClipFlags(srPrivate->drawRegion, &clipMaskState);
  70.     if (clipMaskState == kQ3XClipMaskNotExposed) {
  71.         return (kQ3Success);
  72.     }
  73.  
  74.     /*
  75.      *  Lazy-evaluate the various transforms for the pipeline
  76.      */
  77.     if (SR_UpdatePipeline(srPrivate) == kQ3Failure) {
  78.         return (kQ3Failure);
  79.     }
  80.  
  81.     /*
  82.      *  Highlight state and  color are from the view, unless
  83.      *  overridden by the markerAttributeSet
  84.      */
  85.     highlightState = srPrivate->viewHighlightState;
  86.     color = srPrivate->viewDiffuseColor;
  87.  
  88.     /*
  89.      *  Check if we have a marker attribute set.
  90.      *  If so, then see if we can get a color and highlight state
  91.      *  out of it. 
  92.      */
  93.     if (markerData->markerAttributeSet != NULL) {
  94.         TQ3XAttributeMask    attributeMask;
  95.         
  96.         attributeMask = Q3XAttributeSet_GetMask(markerData->markerAttributeSet);
  97.         
  98.         if (attributeMask & kQ3XAttributeMaskDiffuseColor) {
  99.             Q3AttributeSet_Get(
  100.                 markerData->markerAttributeSet, 
  101.                 kQ3AttributeTypeDiffuseColor, 
  102.                 &color);
  103.         }
  104.         
  105.         if (attributeMask & kQ3XAttributeMaskHighlightState) {
  106.             Q3AttributeSet_Get(
  107.                 markerData->markerAttributeSet, 
  108.                 kQ3AttributeTypeHighlightState, 
  109.                 &highlightState);
  110.         }
  111.     }
  112.     
  113.     /*
  114.      *  If we're highlighting, then see if we can get a highlight color
  115.      *  out of the view's attribute set. Use that as the color, if it's there.
  116.      */
  117.     if (highlightState == kQ3True && 
  118.         (srPrivate->viewHighlightAttributeSet != NULL)) {
  119.         TQ3XAttributeMask    attributeMask;
  120.         
  121.         attributeMask = Q3XAttributeSet_GetMask(srPrivate->viewHighlightAttributeSet);
  122.         
  123.         if (attributeMask & kQ3XAttributeMaskDiffuseColor) {
  124.             Q3AttributeSet_Get(
  125.                 srPrivate->viewHighlightAttributeSet, 
  126.                 kQ3AttributeTypeDiffuseColor, 
  127.                 &color);
  128.         }
  129.     }
  130.  
  131.     {
  132.         TQ3Matrix4x4        *localToDC;
  133.         TQ3RationalPoint4D    *deviceVertices = NULL;
  134.         TQ3RationalPoint4D    *renderVertices = NULL;
  135.         MarkerFunction2D    markerFunc;
  136.         TSRMarkerRasterData    rasterMarker;
  137.         TQ3Point2D            saveDeviceVertices;
  138.         
  139.         /*        
  140.          *  1.0     transform local coordinate vertices to DC
  141.          *    1.5        add in offsets
  142.          *  2.0        clip in DC
  143.          *  3.0     paste bitmap
  144.          */
  145.         TQ3Bitmap    *bitmapPtr = (TQ3Bitmap *)&(markerData->bitmap);
  146.         float        inverseW;
  147.         
  148.         localToDC = &srPrivate->transforms.localToDC;
  149.  
  150.         deviceVertices = malloc(sizeof(TQ3RationalPoint4D));
  151.         if (deviceVertices == NULL) {
  152.             return (kQ3Failure);
  153.         }
  154.  
  155.         /* 1. transform LC to DC */
  156.         Q3Point3D_To4DTransformArray(
  157.             &markerData->location, 
  158.             localToDC,
  159.             deviceVertices, 
  160.             1,
  161.             sizeof(TQ3Point3D), 
  162.             sizeof(TQ3RationalPoint4D));
  163.                                      
  164.         inverseW = 1.0 / deviceVertices->w;
  165.         deviceVertices->x *= inverseW;
  166.         deviceVertices->y *= inverseW;
  167.         deviceVertices->z *= inverseW;
  168.  
  169.         deviceVertices->x += markerData->xOffset;
  170.         deviceVertices->y += markerData->yOffset;
  171.         
  172.         deviceVertices->x = FLOAT_ROUND_TO_LONG_POSITIVE(deviceVertices->x);
  173.         deviceVertices->y = FLOAT_ROUND_TO_LONG_POSITIVE(deviceVertices->y);
  174.         
  175.         /* Check for trivial rejection clipping */
  176.         
  177.         if (deviceVertices->z < srPrivate->clipPlanesInDC[4]                             || 
  178.             deviceVertices->z > srPrivate->clipPlanesInDC[5]                             ||
  179.             deviceVertices->x + bitmapPtr->width - 1 < srPrivate->clipPlanesInDC[0]     ||
  180.             deviceVertices->x > srPrivate->clipPlanesInDC[1]                             ||
  181.             deviceVertices->y + bitmapPtr->height - 1 < srPrivate->clipPlanesInDC[2]     ||
  182.             deviceVertices->y > srPrivate->clipPlanesInDC[3] ) {
  183.             /* 
  184.              *  trivial rejection 
  185.              */
  186.             return (kQ3Success);
  187.         }
  188.  
  189.         /* 
  190.          *  Marker dimensions could be bigger than window so clip 
  191.          *  against all boundaries 
  192.          */
  193.         rasterMarker.bitmap = bitmapPtr;
  194.  
  195.         rasterMarker.startRowSkip    = 0;
  196.         rasterMarker.endRowSkip        = 0;
  197.         
  198.         saveDeviceVertices.x = deviceVertices->x;
  199.         saveDeviceVertices.y = deviceVertices->y;
  200.  
  201.         if (deviceVertices->x < srPrivate->clipPlanesInDC[0]) {
  202.             rasterMarker.startRowSkip = 
  203.                 srPrivate->clipPlanesInDC[0] - deviceVertices->x;
  204.             deviceVertices->x = srPrivate->clipPlanesInDC[0];
  205.         }
  206.         
  207.         if (saveDeviceVertices.x + bitmapPtr->width - 1 > 
  208.                 srPrivate->clipPlanesInDC[1]) {
  209.             rasterMarker.endRowSkip = 
  210.                 saveDeviceVertices.x + bitmapPtr->width - 1 
  211.                                       - srPrivate->clipPlanesInDC[1];
  212.         }
  213.         
  214.         rasterMarker.startLineSkip     = 0;
  215.         rasterMarker.endLineSkip     = 0;
  216.         
  217.         if (deviceVertices->y < srPrivate->clipPlanesInDC[2]) {
  218.             rasterMarker.startLineSkip = 
  219.                     srPrivate->clipPlanesInDC[2] - deviceVertices->y;
  220.             deviceVertices->y = srPrivate->clipPlanesInDC[2];
  221.         }
  222.         
  223.         if (saveDeviceVertices.y + bitmapPtr->height - 1 > 
  224.                 srPrivate->clipPlanesInDC[3]) {
  225.             rasterMarker.endLineSkip = 
  226.                 saveDeviceVertices.y + bitmapPtr->height - 1 
  227.                                            - srPrivate->clipPlanesInDC[3];
  228.         }
  229.         
  230.         /*
  231.          *  Call the appropriate marker rasterization function
  232.          */
  233.         markerFunc = ((TSRRasterFunctions *)
  234.                         (srPrivate->currentRasterFunctions))->markerFunction;
  235.         if ((*markerFunc)(
  236.                 srPrivate, 
  237.                 (TQ3Point3D *)deviceVertices, 
  238.                 &rasterMarker, 
  239.                 &color) == kQ3Failure) {
  240.             if (deviceVertices != NULL) {
  241.                 free(deviceVertices);
  242.                 return (kQ3Failure);
  243.             }
  244.         }
  245.  
  246.         if (deviceVertices != NULL) {
  247.             free(deviceVertices);
  248.         }
  249.     }
  250.     
  251.     
  252.     return (kQ3Success);
  253. }
  254.